package com.longshihan.module_dandu.mvp.ui.activity;

import android.Manifest;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.longshihan.module_base.utils.AppUtil;
import com.longshihan.module_base.utils.FileUtil;
import com.longshihan.module_base.utils.PreferenceUtils;
import com.longshihan.module_dandu.R;
import com.longshihan.module_dandu.dagger.component.DaggerSplashActivityComponent;
import com.longshihan.module_dandu.dagger.module.SplashModule;
import com.longshihan.module_dandu.mvp.contract.SplashContract;
import com.longshihan.module_dandu.mvp.persenter.SplashPersenter;
import com.longshihan.module_dandu.widght.FixedImageView;
import com.longshihan.mvplh.base.BaseMVPActivity;
import com.longshihan.mvplh.dagger.component.AppComponent;
import com.tbruyelle.rxpermissions2.Permission;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

@Route(path = "/dandu/SplashActivity")
public class SplashActivity extends BaseMVPActivity<SplashPersenter> implements SplashContract.View {
    private AppComponent mAppComponent;
    FixedImageView splashImg;

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(int type, String message) {

    }

    @Override
    public void closeActivity() {

    }

    @Override
    public void startLoadMore() {

    }

    @Override
    public void endLoadMore() {

    }

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSplashActivityComponent.builder()
                .appComponent(appComponent)
                .splashModule(new SplashModule(this))
                .build()
                .inject(this);
        mAppComponent = appComponent;
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.dandu_activity_splash;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        splashImg = (FixedImageView) findViewById(R.id.splash_img);
        initStatus();
        doReadFilePermisssion();
    }

    /**
     * 获取权限
     */
    private void doReadFilePermisssion() {
        rxPermissions.requestEach(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_PHONE_STATE
                                 )
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(@NonNull Permission permission) throws Exception {
                        if (permission.granted) {
                            Log.i("permissions", Manifest.permission.READ_CALENDAR + "：获取成功");

                        } else {
                            Log.i("permissions", Manifest.permission.READ_CALENDAR + "：获取失败");
                        }
                    }
                });
        delaySplash();

    }

    private void delaySplash() {
        List<String> picList = FileUtil.getAllAD();
        if (picList.size() > 0) {
            Random random = new Random();
            int index = random.nextInt(picList.size());
            int imgIndex = PreferenceUtils.getValue("splash_img_index", 0);
            if (index == imgIndex) {
                if (index >= picList.size()) {
                    index--;
                } else if (imgIndex == 0) {
                    if (index + 1 < picList.size()) {
                        index++;
                    }
                }
            }
            PreferenceUtils.setValue("splash_img_index", index);
            File file = new File(picList.get(index));
            try {
                InputStream fis = new FileInputStream(file);
                splashImg.setImageDrawable(InputStream2Drawable(fis));
                animWelcomeImage();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {

            }
        } else {
            try {
                AssetManager assetManager = this.getAssets();
                InputStream in = assetManager.open("welcome_default.jpg");
                splashImg.setImageDrawable(InputStream2Drawable(in));
                animWelcomeImage();
                in.close();
                String deviceId = AppUtil.getDeviceId(SplashActivity.this);
                mPresenter.StartTask(deviceId);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void initStatus() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public Drawable InputStream2Drawable(InputStream is) {
        Drawable drawable = BitmapDrawable.createFromStream(is, "splashImg");
        return drawable;
    }

    private void animWelcomeImage() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(splashImg, "translationX", -100F);
        animator.setDuration(1500L).start();
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Intent intent = new Intent(SplashActivity.this, DanduActivitymainActivity2.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }


    @Override
    public boolean useFragment() {
        return false;
    }
}
